import * as config from "../config/config";

const {ccclass, property} = cc._decorator;

@ccclass
export default class RewardItem extends cc.Component {

  @property(cc.Label)
  desc: cc.Label = null;

  @property(cc.Sprite)
  icon: cc.Sprite = null;

  init(id: number) {
    let item = config.getItem(config.getReward(id, 'itemid'));
    this.desc.string = `${item.name}x${config.getReward(id, 'number')}`;
    // 图标
    let icon = this.icon;
    cc.loader.loadRes(`icon/${item.iconid}`, cc.SpriteFrame, function (err, frame) {
      err ? cc.log(err) : (icon.spriteFrame = frame);
    });
    // 碎片
    if (config.ResType.skinPiece === item.type) {
      cc.loader.loadRes(`icon/piece`, cc.SpriteFrame, function (err, frame) {
        if (err) {
        } else {
          let piece = new cc.Node('piece');
          piece.parent = icon.node;
          piece.addComponent(cc.Sprite).spriteFrame = frame;
          piece.x = (icon.node.width - piece.width) / 2;
          piece.y = (icon.node.height - piece.height) / 2;
        }
      });
    }
  }
}
